<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
$(function(){
	var determine = false;
	var roleName = $("#roleName").val();
	if(roleName!="ADMINISTRATOR" || roleName == null){
		determine = true;
		$("#addButton").hide();
		$("#deleteButton").hide();
		$("#editButton").hide();
	}
	
	$("#dialogDiv").dialog({
		title:"外访维护",//标题
		width: 600,//宽度
	    height: 260,//高度
	    top:35,
	    closed: true,//初始关闭
	    cache: false,//缓存
	    modal: true,
	    onClose: function() {
			 $("#dialogIframe").attr("src","");
			} 
	});
	
	$("#addButton").click(function(){
		$("#dialogIframe").attr("src","peopleRecordAdd.action");
		$("#dialogDiv").dialog('open');
	});
	
	$("#deleteButton").click(function(){
		var id = "";
		var checkedObj = $("input[name='id']:checked");
		if(checkedObj.size() == 1){
			id = checkedObj.val();
			$.messager.confirm({
				title:'提示',
				msg:'确认删除吗',
				top:35,
				fn:function(r){
					if(r){
						$.ajax({
							type:'POST',
							url:'peopleRecordDelete.action',
							data:{id:id},
							datatype:'json',
							success:function(msg){
								if(msg.status == "200"){
									$('#peopleRecordList').datagrid({ queryParams:{storiedBuilding:storiedBuilding}});
								}else{
									$.messager.alert("提示","删除失败");
								}
							}
						});
					}
				}
			});
		}else{
			$.messager.alert("提示","选择一条记录请");
		}
	});
	
	$("#editButton").click(function(){
		var id = "";
		var checkedObj = $("input[name='id']:checked");
		if(checkedObj.size() == 1){
			id = checkedObj.val();
			$("#dialogIframe").attr("src","peopleRecordEdit.action?id="+id);
			$("#dialogDiv").dialog('open');
		}else{
			$.messager.alert("提示","选择一条记录");
		}
	});
	
	$("#showButton").click(function(){
		var id = "";
		var checkedObj = $("input[name='id']:checked");
		if(checkedObj.size() == 1){
			id = checkedObj.val();
			$("#dialogIframe").attr("src","peopleRecordShow.action?id="+id);
			$("#dialogDiv").dialog('open');
		}else{
			$.messager.alert("提示","选择一条记录");
		}
	});
	
	var storiedBuilding = $("#storiedBuilding").val();
	$('#peopleRecordList').datagrid({
	    url:'<c:url value="peopleRecordList.action"/>',
	    queryParams:{storiedBuilding:storiedBuilding},//提交参数
	    toolbar:'#carCategoryToolbar',//表头工具栏
	    pagination:true,//显示分页  
	    pagePosition:'bottom',//分页栏出现位置
	    pageNumber:1,//初始页码
	    pageSize:10,//初始每页显示记录数量
	    pageList:[10, 20, 30, 40], 
	    fitColumns:true,
	    selectOnCheck:false,
	    remoteSort: false,
	    singleSelect:false,
	    rownumbers:true,
	    striped:true,
	    singleSelect:false,
	    columns:[[
			{field:'id',title:'',width:20,hidden:determine,formatter: function(value,row,index){
				return '<input name="id" type="checkbox" value="'+value+'" />';
			}},
			{field:'cnName',title:'姓名',align:'center',width:50},
			{field:'content',title:'访问原因',width:300,align:'center'},
			{field:'phoneNumber',title:'电话号码',align:'center',width:80},
			{field:'idCardNumber',title:'身份证号码',align:'center',width:120},
			{field:'time',title:'来访时间',align:'center',width:100,sortable:true,formatter: function(value,row,index){
				var str="";
				if(value){
					str=dataFormatStr(new Date(value),"yyyy-MM-dd hh:mm:ss").replace(" ","<br/>");
				}
				return str;
			}},
	    ]],
	    onClickRow:function(index,row){
		    $("input[type=checkbox]").val([row.id]).attr("checked",true);
		    }
	});
	
});


function closeDialog(){
	var storiedBuilding = $("#storiedBuilding").val();
	$('#peopleRecordList').datagrid({ queryParams:{storiedBuilding:storiedBuilding}});
	$('#dialogDiv').dialog('close');
}